package Demo1;

import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ReflectClassDemo {
    // 在类外进行反射,获取Student对象中的私有成员和方法
    public static void reflectNewInstance(){
        // 创建一个Class对象
        Class<?> classStudent = null;
        try {
            classStudent = Class.forName("Demo1.Student");
            // 得到Class类的实例
            // 获取类的实例擦成了Object类,向下转型
            Student student = (Student)classStudent.newInstance();
            System.out.println(student);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    // 反射私有的构造方法
    public static void reflectPrivateConstructor(){
        Class<?> classStudent = null;
        try {
            classStudent = Class.forName("Demo1.Student");
            // 获取构造方法
            Constructor<?> constructor = classStudent.getDeclaredConstructor(String.class,int.class);
            // 如果是私有的构造方法确定要修改,要设置为true
            constructor.setAccessible(true);
            // 利用构造方法构造对象,继承自Object类->constructor
            Student student = (Student)constructor.newInstance("xiaoming",15);
            System.out.println(student);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    // 获取私有字段
    public static void reflectPrivateField(){
        Class<?> classStudent = null;
        try {
            classStudent = Class.forName("Demo1.Student");
            // 获取字段
            Field filed = classStudent.getDeclaredField("name");
            filed.setAccessible(true);
            // 获取对象
            Student student = (Student)classStudent.newInstance();
            // 修改该对象的字段
            filed.set(student,"caocao");
            System.out.println(student);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    // 获取私有的方法
    public static void reflectPrivateMethod(){
        Class<?> classStudent = null;
        try {
            classStudent = Class.forName("Demo1.Student");
            // 获取私有的方法对象
            Method method = classStudent.getDeclaredMethod("function",String.class);
            // 设置为true可以调用私有方法
            method.setAccessible(true);
            // 获取student对象
            Student student = (Student) classStudent.newInstance();
            // 调用私有的方法
            method.invoke(student,"我是一个反射的参数");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }
    public static void main(String[] args) {
        // reflectNewInstance();
        // reflectPrivateConstructor();
        // reflectPrivateField();
        reflectPrivateMethod();
    }
}
